package com.huahang.fashionpress.service.impl;

import com.huahang.fashionpress.mapper.ImgMapper;
import com.huahang.fashionpress.mapper.ProductMapper;
import com.huahang.fashionpress.model.Img;
import com.huahang.fashionpress.model.Product;
import com.huahang.fashionpress.service.ISingleService;
import com.huahang.fashionpress.vo.ImgVO;
import com.huahang.fashionpress.vo.ProductSingleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 查询一个商品详情并返回方法
 * @author : 王翰聪
 * @since : 2019/08/24
 * @version : 1.0
 */

@Service
public class SingleServiceImpl implements ISingleService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ImgMapper imgMapper;

    /**
     * 通过productId 获取一个商品的详情
     * @param productID 待查询商品id
     * @return 一个商品详细信息
     */
    @Override
    public ProductSingleVO getSingleProduct(String productID) {
        //查询商品信息
        Product product = productMapper.selectById(productID);
        //查询所有商品图片信息
        List<Img> imgList = imgMapper.getSingleProductImgList(productID, 1);
        //封装所有图片信息
        List<ImgVO> imgVOList = new ArrayList<ImgVO>();
        for(Img img : imgList){
            ImgVO imgVO = new ImgVO();
            imgVO.setImgName(img.getImgName());
            imgVOList.add(imgVO);
        }
        //封装商品信息
        ProductSingleVO productSingleVO = new ProductSingleVO();
        productSingleVO.setProductId(productID);
        productSingleVO.setProductName(product.getProductName());
        productSingleVO.setCategoryId(product.getCategoryId());
        productSingleVO.setProductDescribe(product.getProductDescribe());
        productSingleVO.setProductInventory(product.getProductInventory());
        productSingleVO.setProductPrice(product.getProductPrice());
        productSingleVO.setImgList(imgVOList);

        return productSingleVO;
    }
}
